package com.renhuikeji.taobaoweb.activitys;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.renhuikeji.taobaoweb.AndroidShare;
import com.renhuikeji.taobaoweb.MywebChromeClient;
import com.renhuikeji.taobaoweb.R;
import com.renhuikeji.taobaoweb.inter.Webloading;
import com.renhuikeji.taobaoweb.widget.CustomDialog;
import com.renhuikeji.taobaoweb.widget.CustomWebView;
import com.renhuikeji.taobaoweb.zxing.DecodeImage;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class MainActivity extends Activity implements CustomWebView.LongClickCallBack, Webloading {

    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private CustomWebView mCustomWebView;
    private CustomDialog mCustomDialog;
    private ArrayAdapter<String> adapter;
    private boolean isQR;//判断是否为二维码
    private Result result;//二维码解析结果
    private String url;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        ButterKnife.bind(this);
      /*  MyDialog dialog = new MyDialog();
        dialog.show(getFragmentManager(), "");*/
        initWebView();
        requestPermission();

    }

    private void initWebView() {
        // 初始WebView化控件
        mCustomWebView = new CustomWebView(this, this, this);
        mCustomWebView.setWebChromeClient(new MywebChromeClient(this));
        //这里借用翔哥的博客
        url = "http://www.sqtb5188.com/mobile";
//      url="http://swb.liexianghudong.com/mobile";
        mCustomWebView.loadUrl(url);//加载页面
        mCustomWebView.setFocusable(true);
        mCustomWebView.setFocusableInTouchMode(true);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addContentView(mCustomWebView, lp);
        mCustomWebView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onLongClickCallBack(final String imgUrl) {
        url = imgUrl;
        // 获取到图片地址后做相应的处理
        MyAsyncTask mTask = new MyAsyncTask();
        mTask.execute(imgUrl);
        showDialog();

    }

    private void requestPermission() {
        RxPermissions rxPermissions = new RxPermissions(MainActivity.this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                        } else {
                            Toast.makeText(MainActivity.this, "已拒绝申请相关权限！", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

    /**
     * 判断是否为二维码
     * param url 图片地址
     * return
     */
    private boolean decodeImage(String sUrl) {
        result = DecodeImage.handleQRCodeFormBitmap(getBitmap(sUrl));
        if (result == null) {
            isQR = false;
        } else {
            isQR = true;
        }
        return isQR;
    }

    @Override
    public void finishLoading() {
        mCustomWebView.setVisibility(View.VISIBLE);
        image.setVisibility(View.GONE);
        tvConfirm.setVisibility(View.GONE);
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        mCustomWebView.setVisibility(View.VISIBLE);
        image.setVisibility(View.GONE);
        tvConfirm.setVisibility(View.GONE);
    }


    public class MyAsyncTask extends AsyncTask<String, Void, String> {


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if (isQR) {
                handler.sendEmptyMessage(0);
            }


        }

        @Override
        protected String doInBackground(String... params) {
            decodeImage(params[0]);
            return null;
        }
    }

    /**
     * 根据地址获取网络图片
     *
     * @param sUrl 图片地址
     * @return
     * @throws IOException
     */
    private Bitmap shareBitmap;

    public Bitmap getBitmap(String sUrl) {
        try {
            URL url = new URL(sUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                shareBitmap = bitmap;
                saveMyBitmap(bitmap, "code");//先把bitmap生成jpg图片
                return bitmap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 显示Dialog
     * param v
     */
    private void showDialog() {
        initAdapter();
        mCustomDialog = new CustomDialog(this) {
            @Override
            public void initViews() {
                // 初始CustomDialog化控件
                ListView mListView = (ListView) findViewById(R.id.lv_dialog);
                mListView.setAdapter(adapter);
                mListView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 点击事件
                        AndroidShare share = new AndroidShare(MainActivity.this);
                        switch (position) {
                            case 0:
                                share.shareWeChatFriend("", "", 1, shareBitmap);
//								sendToFriends();//把图片发送给好友
                                closeDialog();
                                break;
                            case 1:
                                share.shareQQFriend("", "", 1, shareBitmap);
//								sendToFriends();//把图片发送给好友
                                closeDialog();
                                break;
                            case 2:
                                saveImageToGallery(MainActivity.this);
                                closeDialog();
                                break;
                            case 3:
                                goIntent();
                                closeDialog();
                                break;
                        }

                    }
                });
            }
        };
        mCustomDialog.show();
    }

    /**
     * 初始化数据
     */
    private void initAdapter() {
        adapter = new ArrayAdapter<String>(this, R.layout.item_dialog);
        adapter.add("分享到微信");
        adapter.add("分享到QQ");
        adapter.add("保存到手机");
        /*adapter.add("收藏");*/
    }

    /**
     * 是二维码时，才添加为识别二维码
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                if (isQR) {
                    adapter.add("识别图中二维码");
                }
                adapter.notifyDataSetChanged();
            }
        }

        ;
    };

    /**
     * 发送给好友
     */
    private void sendToFriends() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        Uri imageUri = Uri.parse(file.getAbsolutePath());
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, imageUri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getTitle()));
    }

    /**
     * bitmap 保存为jpg 图片
     *
     * @param mBitmap 图片源
     * @param bitName 图片名
     */
    public void saveMyBitmap(Bitmap mBitmap, String bitName) {
        file = new File(Environment.getExternalStorageDirectory() + "/" + bitName + ".jpg");
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 先保存到本地再广播到图库
     */
    public void saveImageToGallery(Context context) {

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), "code", null);
            // 最后通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
                    + file)));
            Toast.makeText(MainActivity.this, "图片保存成功", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void goIntent() {
        Uri uri = Uri.parse(result.toString());
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    private long exitTime = -3000;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.d("CCC", "" + (System.currentTimeMillis() - exitTime));
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                initWebView();
                exitTime = System.currentTimeMillis();
            } else {
                System.exit(0);
            }
//          webview.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
